import webapp2

from google.appengine.api import users

from google.appengine.ext import db

class NgoUser(db.Model):
  name = db.StringProperty()
  

#store 3 users
def storeNgoUsers():
	NgoUser(name='tvkitcha').put()
	NgoUser(name='nareshvgiri').put()

def ngouser_key(ngouser_name=None):
  """Constructs a Datastore key for a Guestbook entity with guestbook_name."""
  return db.Key.from_path('NgoUser', ngouser_name or 'no_user')
	
class MainPage(webapp2.RequestHandler):
	def get(self):
		user = users.get_current_user()
		if not user:
			self.redirect(users.create_login_url("/"))
			# if user is still None?
			if not user:
				user = users.User("tvkitcha@gmail.com")
			
		#storeNgoUsers()
		
		#check if user is an NgoUser
		ngousers = db.GqlQuery("SELECT * "
								"FROM NgoUser "
								"WHERE name = :1 ",
								user.nickname())                
		
		if ngousers.count() == 0:
			storeNgoUsers()
			
		#check if user is an NgoUser
		ngousers = db.GqlQuery("SELECT * "
                            "FROM NgoUser "
                            "WHERE name = :1 ",
							user.nickname())		
							
		ngouserexists = ngousers.count() > 0
		
		if ngouserexists:
			self.response.headers['Content-Type'] = 'text/plain'
			self.response.out.write('Hello, NGO Sansaar! welcomes valid user ' + user.nickname())
		else:
			self.response.headers['Content-Type'] = 'text/plain'
			self.response.out.write('Hello, please register in NGO Sansaar! : ' + user.nickname())

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)
